#include "stdafx.h"
#include "Kinect3DData.h"

Kinect3DData::Kinect3DData()
{
	connect(&m_Analysis, SIGNAL(sign_ResDataArrived()), this, SLOT(DataArrived()));
	connect(&m_Analysis, SIGNAL(sign_CalMeshFinal()), this, SLOT(CalMeshFinal()));
	connect(&m_Analysis, SIGNAL(sign_StateChange(int, float)), this, SLOT(FaceStateChange(int, float)));
	connect(&m_Analysis, SIGNAL(sign_ProgressChange(float, QString)), this, SLOT(ProgressChange(float, QString)));
	m_Analysis.StartCollect();
	UpdateState(STATE_WAIT_START);
}

//void Kinect3DData::Start()
//{
//	m_Analysis.Start();
//}
//void Kinect3DData::Stop()
//{
//	m_Analysis.Stop();
//
//}
//bool Kinect3DData::GetCurrentData(DataAnalysis::suResData&data, bool isRemove)
//{
//	return m_Analysis.GetCurrentData(data, isRemove);
//}

void Kinect3DData::ReStart()
{
	m_Analysis.StartCollect();
	UpdateState(STATE_WAIT_START);
}

void Kinect3DData::PrepareReStarDataCollect()
{
	m_Analysis.PrepareReStarDataCollect();
	UpdateState(STATE_WAIT_START);
}

void Kinect3DData::Start()
{
	if (m_state == STATE_WAIT_START)
	{
		shared_ptr<KinectDataCollection> pKinect = KinectDataCollection::GetInstance();
		connect(pKinect.get(), SIGNAL(sign_FaceCollectEnd()), this, SLOT(FaceCollectEnd()));
		m_Analysis.Start();
		UpdateState(STATE_IN_CHECK);
	}

}

void Kinect3DData::Stop()
{
	if (m_state == STATE_IN_CHECK)
	{
		shared_ptr<KinectDataCollection> pKinect = KinectDataCollection::GetInstance();
		disconnect(pKinect.get(), SIGNAL(sign_FaceCollectEnd()), this, SLOT(FaceCollectEnd()));
		m_Analysis.Stop();
		UpdateState(STATE_IN_CAL_MESH);
	}

}

bool Kinect3DData::GetCurrentData(DataAnalysis::suResData&data, bool isRemove)
{
	return m_Analysis.GetCurrentData(data, isRemove);
}

//QString Kinect3DData::GetFaceInfo()
//{
//	return m_Analysis.GetCurrentFaceInfo();
//}

shared_ptr<pcl::PolygonMesh> Kinect3DData::GetFinalMesh() { return m_Analysis.GetFinalMesh(); }

Kinect3DData::emState Kinect3DData::GetState() { return m_state; }


void Kinect3DData::DataArrived()
{
	static int inx = 0;

	++inx;
	emit sign_ResDataArrived();
}

void Kinect3DData::CalMeshFinal()
{
	printf("CalMeshFinal first\n");
	UpdateState(STATE_CHECK_END);
	emit sign_Kinect3DData_CalMeshFinal();

}

void Kinect3DData::FaceStateChange(int state, float fEndTime)
{
	if (state == DataAnalysis::STATE_OK)
	{
		emit sign_FaceCollectEnd();
	}

	if (state == DataAnalysis::STATE_MovingTooFast && fEndTime <= 0)
	{
		emit sign_NeedReStart();
	}

	emit sign_FaceStateChange(state, fEndTime);


}
